﻿using System;
using System.Collections.Generic;
using System.Windows;
using Microsoft.Popfly.GameCreator.GameEngine.Events;

namespace Microsoft.Popfly.GameCreator.GameEngine.Behaviors
{
    public interface IReverseableMotion : IBehavior
    {
        void Reverse();
    }
    public abstract class MotionBehavior : IReverseableMotion
    {
        protected BehaviorOwner m_owner;
        protected double m_offset;
        protected bool m_isOriginalDirection = true;


        public void Reverse()
        {
            m_offset = -m_offset;
            if (m_isOriginalDirection)
            {
                m_isOriginalDirection = false;
            }
            else
            {
                m_isOriginalDirection = true;
            }
        }

        public abstract bool ReachedDistanceFromStart(DistanceEventChecker distanceChecker, double distance);

        protected double XDistanceFromStart(DistanceEventChecker distanceChecker) { return Math.Abs(m_owner.X - distanceChecker.StartingX); }
        protected double YDistanceFromStart(DistanceEventChecker distanceChecker) { return Math.Abs(m_owner.Y - distanceChecker.StartingY); }
        protected double DistanceSquaredFromStart(DistanceEventChecker distanceChecker) { return MathUtils.DistanceSquared(m_owner.X, m_owner.Y, distanceChecker.StartingX, distanceChecker.StartingY); }

        protected double RotationDistanceFromStart(DistanceEventChecker distanceChecker)
        {
            double movedDistance = ((Actor)m_owner).Rotation - distanceChecker.LastRotationValue;
            if (movedDistance > 180)
            {
                movedDistance = movedDistance - 360;
            }

            if (movedDistance < - 180)
            {
                movedDistance = movedDistance + 360;
            }

            distanceChecker.RotationDistanceFromStart += movedDistance;

            distanceChecker.LastRotationValue = ((Actor)m_owner).Rotation;
            return Math.Abs(distanceChecker.RotationDistanceFromStart);
        }


        public MotionBehavior(BehaviorOwner owner, double offset)
        {
            m_owner = owner;
            m_offset = offset;
        }

        public abstract void Invoke(BehaviorOwner sender, Microsoft.Popfly.GameCreator.GameEngine.Events.PGCEventArgs args);
    }
}
